//
//  CYLPlusButtonSubClass.m
//  YLHelper
//
//  Created by she1323 on 2018/4/10.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "CYLPlusButtonSubClass.h"
#import "LLWPlusPopView.h"
#import "LKAllScheduleViewController.h"
#import "LKAddWorkCustomViewController.h"
#import "LKAddWorkCustomViewController.h"
#import "LKConfirmCustomInforViewController.h"
#import "LKWorkViewController.h"
#import "AppDelegate.h"
@implementation CYLPlusButtonSubClass

+(void)load{
    [super registerPlusButton];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.adjustsImageWhenHighlighted = NO;
    }
    return self;
}
//上下结构的 button
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 控件大小,间距大小
    // 注意：一定要根据项目中的图片去调整下面的0.7和0.9，Demo之所以这么设置，因为demo中的 plusButton 的 icon 不是正方形。
    CGFloat const imageViewEdgeWidth   = self.bounds.size.width * 0.7;
    CGFloat const imageViewEdgeHeight  = imageViewEdgeWidth * 0.9;
    
    CGFloat const centerOfView    = self.bounds.size.width * 0.5;
    CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;
    CGFloat const verticalMargin  = (self.bounds.size.height - labelLineHeight - imageViewEdgeHeight) * 0.5;
    
    // imageView 和 titleLabel 中心的 Y 值
    CGFloat const centerOfImageView  = verticalMargin + imageViewEdgeHeight * 0.5;
    CGFloat const centerOfTitleLabel = imageViewEdgeHeight  + verticalMargin * 2 + labelLineHeight * 0.5 + 5;
    
    //imageView position 位置
    self.imageView.bounds = CGRectMake(0, 0, imageViewEdgeWidth, imageViewEdgeHeight);
    self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
    
    //title position 位置
    self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
    self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
}
#pragma mark -
#pragma mark - CYLPlusButtonSubclassing Methods

/*
 *
 Create a custom UIButton with title and add it to the center of our tab bar
 *
 */
//+ (id)plusButton {
//    CYLPlusButtonSubClass *button = [[CYLPlusButtonSubClass alloc] init];
//    ///中间按钮图片
//    UIImage *buttonImage = [UIImage imageNamed:@"product"];
//    [button setImage:buttonImage forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//
//    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
//
//    button.titleLabel.font = [UIFont systemFontOfSize:9.5];
//    [button sizeToFit];
//    // or set frame in this way `button.frame = CGRectMake(0.0, 0.0, 250, 100);`
////        button.frame = CGRectMake(0.0, 0.0, 250, 100);
////        button.backgroundColor = [UIColor redColor];
//
//    // if you use `+plusChildViewController` , do not addTarget to plusButton.
//    [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
//    return button;
//}
+(id)plusButton{
    UIImage *buttonImage = [UIImage imageNamed:@"jia"];
    UIImage *highlightImage = [UIImage imageNamed:@"jia"];
    CYLPlusButtonSubClass* button = [CYLPlusButtonSubClass buttonWithType:UIButtonTypeCustom];
    
        button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
        button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
        [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
        [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
    
        return button;
}
-(void)clickPublish{
    WS(weakSelf)
    NSMutableArray *imgs = [[NSMutableArray alloc] init];
    [imgs addObject:@"tjdd"];
    [imgs addObject:@"tjkh"];
    [imgs addObject:@"xjrc"];
    NSArray *titles = @[@"添加订单", @"添加客户", @"新建日程"];

    [LLWPlusPopView showWithImages:imgs titles:titles selectBlock:^(NSInteger index) {
        id responder = self.nextResponder;
        while (![responder isKindOfClass: [UIViewController class]] && ![responder isKindOfClass: [UIWindow class]])
        {
            responder = [responder nextResponder];
        }
        if ([responder isKindOfClass: [UIViewController class]])
        {
            // responder就是view所在的控制器
            // do something
                       UIViewController *currentVc = responder;
                    if (index == 0) {
//                        LKConfirmCustomInforViewController *vc = [[LKConfirmCustomInforViewController alloc] init];
//                        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//                        [currentVc presentViewController:nav animated:YES completion:nil];
//                    self.ta
//                        LKWorkViewController *vc = [[LKWorkViewController alloc] init];
////                        [[currentVc presentViewController:vc animated:YES]; completion:nil];
//            [UITabBar appearance]
                        
                        // 这是从一个模态出来的页面跳到tabbar的某一个页面
                        
                        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                        
                        UITabBarController *tabViewController = (UITabBarController *) appDelegate.window.rootViewController;
                        
                        [tabViewController setSelectedIndex:1];
                        UINavigationController *navvc = tabViewController.viewControllers[1];
                        LKWorkViewController *vc = navvc.viewControllers[0];
                        
                        NSLog(@"%@",tabViewController.viewControllers);
                        NSLog(@"%@",vc.lalala);
                        [vc.flipView selectIndex:1];
                        [vc.segment selectIndex:2];
                        
                    }else if (index == 1){
                        
                        LKAddWorkCustomViewController *vc = [[LKAddWorkCustomViewController alloc] init];
                        vc.specialAdd = YES;
                        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                        [currentVc presentViewController:nav animated:YES completion:nil];
                    }else{
                        LKAllScheduleViewController *vc = [[LKAllScheduleViewController alloc] init];
                        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                        [currentVc presentViewController:nav animated:YES completion:nil];
                    }

        }
//        if (index == 0) {
//            LKAllScheduleViewController *vc = [[LKAllScheduleViewController alloc] init];
//
//        }else if (index == 1){
//
//        }else{
//
//        }
        NSLog(@"index:%zi", index);
    }];
}
/**
 *  返回当前视图的控制器
 */
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
#pragma mark -放大按钮的代理方法

//+(NSUInteger)indexOfPlusButtonInTabBar{
//    return 2;
//}
+(CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight{
    return 0.5;
}
+(CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)tabBarHeight{
    if (iPhoneX) {
        return -10;
    }
    return 0;
}

@end
